package top.tenebrous.yzl.im.handler.channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import top.tenebrous.yzl.im.enums.ChannelType;
import top.tenebrous.yzl.im.factory.channel.ChannelFactory;
import top.tenebrous.yzl.im.message.Message;
import top.tenebrous.yzl.im.message.MessageBody;
import top.tenebrous.yzl.im.utils.WebSocketUtils;

/**
 * @author yazhang6
 */
@Slf4j
@Component
public class CloseChannel implements Channel<MessageBody>, InitializingBean {

    @Autowired
    private ChannelFactory<MessageBody> channelFactory;

    @Override
    public void afterPropertiesSet() throws Exception {
        channelFactory.register(ChannelType.CLOSE_CHANNEL, this);
    }

    @Override
    public void doHandler(WebSocketSession session, String type, MessageBody messageBody) {
        // websocket连接关闭的消息
        WebSocketUtils.removeSession(session);
        log.info("{}:已主动下线 当前客户端状态:{}", messageBody.getFrom(), messageBody.getContent());
    }
}
